<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="配置名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="settingName">
              <a-input v-model="model.settingName" placeholder="请输入配置名"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="纸张类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paperType">
              <j-dict-select-tag
                v-model="model.paperType"
                dict-code="paper_type"
                placeholder="请输入纸张类型"
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="纸张宽度(单位：mm)"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="paperWidth"
            >
              <a-input-number v-model="model.paperWidth" placeholder="请输入纸张宽度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="纸张高度(单位：mm)"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="paperHeight"
            >
              <a-input-number v-model="model.paperHeight" placeholder="请输入纸张高度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="纸张页边距(单位：mm)"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="paperPadding"
            >
              <a-input-number v-model="model.paperPadding" placeholder="请输入纸张页边距" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="标签边距(单位：mm)"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="labelPadding"
            >
              <a-input-number v-model="model.labelPadding" placeholder="请输入标签边距" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="标签间距(单位：mm)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labelGap">
              <a-input-number v-model="model.labelGap" placeholder="请输入标签间距" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="标签宽度(单位：mm)"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="labelWidth"
            >
              <a-input-number v-model="model.labelWidth" placeholder="请输入标签宽度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="标签高度(单位：mm)"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="labelHeight"
            >
              <a-input-number v-model="model.labelHeight" placeholder="请输入标签高度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="一行多少个标签" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labelCol">
              <a-input-number v-model="model.labelCol" placeholder="请输入一行多少个标签" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'PrintSettingsForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        paperType: '1'
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        settingName: [
          {
            required: true,
            message: '请输入配置名',
            trigger: 'blur'
          }
        ],
        paperType: [
          {
            required: true,
            message: '请选择纸张类型',
            trigger: 'change'
          }
        ],
        paperWidth: [
          {
            required: true,
            message: '请输入纸张宽度',
            trigger: 'blur'
          }
        ],
        paperHeight: [
          {
            required: true,
            message: '请输入纸张高度',
            trigger: 'blur'
          }
        ],
        paperPadding: [
          {
            required: true,
            message: '请输入纸张页边距',
            trigger: 'blur'
          }
        ],
        labelPadding: [
          {
            required: true,
            message: '请输入标签边距',
            trigger: 'blur'
          }
        ],
        labelGap: [
          {
            required: true,
            message: '请输入标签间距',
            trigger: 'blur'
          }
        ],
        labelWidth: [
          {
            required: true,
            message: '请输入标签宽度',
            trigger: 'blur'
          }
        ],
        labelHeight: [
          {
            required: true,
            message: '请输入标签高度',
            trigger: 'blur'
          }
        ],
        labelCol: [
          {
            required: true,
            message: '请输入一行多少个标签',
            trigger: 'blur'
          }
        ]
      },
      url: {
        add: '/label/printSettings/add',
        edit: '/label/printSettings/edit',
        queryById: '/label/printSettings/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>
